package ${serviceImplPkg};

<#compress>
import lombok.RequiredArgsConstructor;
import cn.lycode.common.core.domain.model.SelectIdsDTO;
import cn.lycode.common.core.page.PageResult;
import cn.lycode.common.core.page.PageUtil;
import cn.lycode.common.enums.ErrorEnum;
import org.springframework.stereotype.Service;
import ${servicePkg}.${serviceClassName};
import ${entityPkg}.${entityClassName};
import ${mapperPkg}.${mapperClassName};
import ${dtoPkg}.${dtoAddClassName};
import ${dtoPkg}.${dtoUpdateClassName};
import ${dtoPkg}.${dtoListClassName};
import ${voPkg}.${voClassName};
import java.util.ArrayList;
import java.util.List;

<#if GeneratorInfo.hasImport == "1">
    import ${dtoPkg}.${dtoImportClassName};
    import org.springframework.web.multipart.MultipartFile;
    import cn.lycode.common.core.domain.model.ExcelResult;
</#if>

<#if GeneratorInfo.hasExport == "1">
    import jakarta.servlet.ServletOutputStream;
    import jakarta.servlet.http.HttpServletResponse;
</#if>
<#if GeneratorInfo.hasImport == "1" || GeneratorInfo.hasExport == "1">
    import cn.lycode.common.utils.ExcelUtils;
    import lombok.SneakyThrows;
</#if>

</#compress>

/**
 * ${tableComment} 服务实现类
 * @since ${datetime}
 * @author ${author}
 */
@Service
@RequiredArgsConstructor
public class ${serviceImplClassName} implements ${serviceClassName} {

<#assign mapperName = lower_case_first_letter(mapperClassName)>
<#assign entityName = lower_case_first_letter(entityClassName)>
    private final ${mapperClassName} ${mapperName};


    //******************自动生成基本增删改查、Excel导入导出*******************//
    @Override
    public PageResult<${voClassName}> listPage(${dtoListClassName} dto){
        PageUtil.startPage(dto);
        List<${entityClassName}> list = ${mapperName}.select${entityClassName}List(dto);
        List<${voClassName}> voList = new ArrayList<>();
        for (${entityClassName} ${entityName} : list) {
            voList.add(${entityName}.buildVO());
        }
        return PageUtil.getPageResult(list, voList);
    }


    @Override
    public int add(${dtoAddClassName} dto){
        ${entityClassName} ${camelClassName} = dto.buildAddEntity();
        <#if hasUniqueValidField == true>
        // TODO 唯一性校验
        </#if>
        return ${mapperName}.insert${entityClassName}(${camelClassName});
    }

    @Override
    public ${voClassName} detail(Long id){
        ${entityClassName} ${camelClassName} = ${mapperName}.select${entityClassName}ById(id);
        ErrorEnum.INVALID_ID.assertNotNull(${camelClassName});
        return ${camelClassName}.buildVO();
    }

    @Override
    public int update(${dtoUpdateClassName} dto){
        ${entityClassName} ${camelClassName} = dto.buildUpEntity();
        <#if hasUniqueValidField == true>
        // TODO 唯一性校验
        </#if>
        return ${mapperName}.update${entityClassName}(${camelClassName});
    }

    @Override
    public void remove(SelectIdsDTO dto){
        ErrorEnum.INVALID_ID.assertFalse(dto.getIds().isEmpty());
        ${mapperName}.remove${entityClassName}ByIds(dto.getIds());
    }

    <#if GeneratorInfo.hasImport == "1">
    @SneakyThrows
    @Override
    public void importExcel(MultipartFile file) {
        ExcelResult<${dtoImportClassName}> excelResult = ExcelUtils.importExcel(file.getInputStream(), ${dtoImportClassName}.class, true);
        List<${dtoImportClassName}> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
    }
    </#if>

    <#if GeneratorInfo.hasExport == "1">
    @SneakyThrows
    @Override
    public void exportExcel(${dtoListClassName} dto, HttpServletResponse response) {
        List<${voClassName}> list = list(dto);
        ServletOutputStream os = response.getOutputStream();
        ExcelUtils.exportExcel(list, "${functionName}", ${voClassName}.class, os);
    }
    </#if>

<#compress>
}




<#-- 内建函数，实现首字母小写 -->
<#function lower_case_first_letter str>
    <#return str?substring(0, 1)?lower_case + str?substring(1)>
</#function>
</#compress>